package mobi.hami.huapin.service.impl;

import mobi.hami.huapin.base.Result;
import mobi.hami.huapin.dao.CreateNoteAuditMapper;
import mobi.hami.huapin.dao.NoteInfoMapper;
import mobi.hami.huapin.model.CreateNoteAudit;
import mobi.hami.huapin.model.NoteInfo;
import mobi.hami.huapin.service.ManagementUGCService;
import mobi.hami.huapin.util.Results;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class ManagementUGCServiceImpl implements ManagementUGCService {

    private static final Logger logger = LoggerFactory.getLogger(ManagementServiceImpl.class);

    @Autowired
    JdbcTemplate jdbcTemplate;

    @Autowired
    CreateNoteAuditMapper createNoteAuditMapper;

    @Autowired
    NoteInfoMapper noteInfoMapper;


    /**
     * 多条件查询
     * @param parms
     * @return
     */
    public List<CreateNoteAudit> selectWitchParam(Map<String, Object> parms){
        return createNoteAuditMapper.selectWitchParam(parms);
    }
    /**
     * 拒绝操作
     */
    public Result refuse(Integer id){
        int i = createNoteAuditMapper.updateStatus(id,1,0,new Date(),"审核未通过");
        if(i > 0){
            return Results.successWithStatus(0,"操作成功");
        }
        return Results.failureWithStatus(1,"操作失败");
    }

    /**
     * 同意操作
     * @param  id 审核记录z主键
     * @param space_id 空間id
     * @param  creator_id 创建者id
     * @param  comtent 纸条内容
     */
    public Result agree(Integer id,Integer space_id,Integer creator_id,String comtent){
        NoteInfo queryNoteInfo = new NoteInfo();
        queryNoteInfo.setComtent(comtent.trim());
        queryNoteInfo.setCreator_id(creator_id);
        queryNoteInfo.setSpace_id(space_id);
        NoteInfo old = noteInfoMapper.selectOne(queryNoteInfo);

        if(null == old){
            NoteInfo noteInfo = new NoteInfo(comtent,new Date(),creator_id,space_id,0L,0L,0,0);
            int n = noteInfoMapper.insert(noteInfo);
        }
        if(null != old){
            if(old.getStatus() == 0){
                System.out.println("纸条库存在且在线 不做任何操作");
            }
            if(old.getStatus() == 1){
                System.out.println("纸条库存在且不在在线 则重新上线");
                old.setStatus(0);
                noteInfoMapper.updateByPrimaryKey(old);
            }
        }
        int m = createNoteAuditMapper.updateStatus(id,2,0,new Date(),"审核通过");
        if(m > 0){
            return Results.successWithStatus(0,"操作成功");
        }
        return Results.failureWithStatus(1,"操作失败");
    }


}
